using System;
using System.Collections.Generic;
using System.Text;
using Petra.Gis.Geometry;
using System.Text.RegularExpressions;
using Petra.Gis.Geometry.Support;
using Petra.Gis.DataServices.WKT.Support;

namespace Petra.Gis.DataServices.WKT
{
  public partial class GeometryFactory
  {
    /***********************************************
     * 28.8.2007
     * 
     * creates linestringGeometry
     * from intergraph blob structure
     * 
     * asida@centrum.cz, initial implementation
     *************************************************/
    //private static GeometryBase ToLineString(ref string inputStr, System.Type parentGeomType)
    private static GeometryBase ToLineString(RegexGeometry rg, System.Type parentGeomType)
    {

      //rgp = new RegexGeometryParser(WKTGeometryType.LineString);
      //rg = rgp.ParseGeometry(ref inputStr);

      CoordinateList Coords = CoordinateListConverter.ToCoordinateList(rg, Dimension.dim2D);
      LineString oGeom = new LineString();
      oGeom.coordList = Coords;

      return oGeom;



      /*
      //prepare regex expr.
      string pattern = @"(?<whitespace>\s*)" +
        @"(?<geometryname>" + getWktName[WKTGeometryType.LineString] + ")" +
        @"(?<openbracket>\s*\()" +
        @"(?<coordinate>(\s*|\s*\,{1}\s*|\s+)\-?(\d*\.?\d+|\d+\.?\d*)([eE][+-]?\d+)?)+" +
        @"(?<closebracket>\s*\))"; //+
      //@"(?<invalid>[^\s]+)"; //put all regex exps here to avoid invalid chars in input string

      Match m;

      Regex r = new Regex(pattern);

      m = r.Match(inputStr);
      if (m.Length < 1)
        throw new ArgumentException("invalid input string for wkt", "inputStr");


      CoordinateList Coords = CoordinateListConverter.ToCoordinateList(m.Groups["coordinate"], Dimension.dim2D);
      LineString oGeom = new LineString();
      oGeom.coordList = Coords;

      return oGeom;
       */
    }




    private static LineString ToLineString(RegexGeometry rg, WKTGeometryType parentGeomType)
    {
      CoordinateList Coords = CoordinateListConverter.ToCoordinateList(rg, Dimension.dim2D);
      LineString oGeom = new LineString();
      oGeom.coordList = Coords;

      return oGeom;

    
    }

    /*
    private static LineString ToLineString(CaptureCollection cc, Capture aproxStart, Capture aproxEnd, System.Type parentGeomType)
    {

      int _start, _end;
      //get num of coords
      //get begin coord index
      for (_start = 0; _start < cc.Count; _start++)
        if (aproxStart.Index < cc[_start].Index)
          break;

      //get end coord index
      for (_end = _start; _end < cc.Count; _end++)
        if (aproxEnd.Index < cc[_end].Index)
        {
          _end--;
          break;
        }

      LineString _geom = new LineString();
      CoordinateList Coords = CoordinateListConverter.ToCoordinateList(cc, _start, _end, Dimension.dim2D);
      _geom.coordList = Coords;
      return _geom;

    }
     */



    /***********************************************
     * 28.8.2007
     * 
     * creates intergraph blob structure 
     * from linestringGeometry
     * 
     * asida@centrum.cz, initial implementation
     *************************************************/

    private static string LineStringToString(GeometryBase geometry)
    {
      LineString _linestring = (LineString)geometry;

      System.IO.StringWriter mStrWriter = new System.IO.StringWriter();


      mStrWriter.Write(WKTGeometryType.LineString.ToString());
      mStrWriter.Write("(");
      mStrWriter.Write(CoordinateListConverter.ToString(_linestring.coordList, Dimension.dim2D));
      mStrWriter.Write(")");

      return mStrWriter.ToString();

    }
  }
}
